package msr.frontend;

import java.util.Map;

import msr.calculate.Calculation;
import msr.calculate.ShipFactory;
import msr.service.OracleService;
import msr.util.RequestAction;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class CalshipAction extends ActionSupport {

	private String productcapacity;
	private String type;
	private String productweight;
	private String actionType;
	private String newweight;
	private String newquantity;
	private int rtweight;
	private int rtquantity;
	private int nokisaki;
	private int seqno;
	// test
	private String jichikucd;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	OracleService oService = new OracleService();

	/**
	 * get data from ajax request , after calculate
	 * */
	public String execute() {
		return SUCCESS;
	}

	public String callAjaxCalculation() {
		ShipFactory sFactory = new ShipFactory(type);
		Calculation cal = sFactory.getCalculation();
		if (actionType.equals(RequestAction.TYPE_GET_QUANTITY)) {
			rtweight = cal.getWeight(Integer.parseInt(productweight),
					Integer.parseInt(newquantity));
		} else if (actionType.equals(RequestAction.TYPE_GET_WEIGHT)) {
			if (type.equals(RequestAction.BARA_TYPE)) {
				Map<String, Object> session = ActionContext.getContext()
						.getSession();
				String username = (String) session.get("username");
				nokisaki = cal.getNokisaki(username, jichikucd);
				rtweight = Integer.parseInt(newweight);
			} else
				rtquantity = cal.getQuantity((Integer.parseInt(productweight)),
						Integer.parseInt(newweight));
		}

		return SUCCESS;
	}

	/**
	 * get type of capacity
	 * 
	 * @return type
	 * */
	public String callAjaxType() {
		type = oService.checkType(productcapacity);
		if (type != null) {
			actionType = RequestAction.TYPE_CHECK_CAPACITY;
			// get data
			return SUCCESS;
		} else
			return ERROR;

	}

	public int getNokisaki() {
		return nokisaki;
	}

	public void setNokisaki(int nokisaki) {
		this.nokisaki = nokisaki;
	}

	public String getProductweight() {
		return productweight;
	}

	public void setProductweight(String productweight) {
		this.productweight = productweight;
	}

	public String getType() {
		return type;
	}

	public String getNewweight() {
		return newweight;
	}

	public void setNewweight(String newweight) {
		this.newweight = newweight;
	}

	public String getNewquantity() {
		return newquantity;
	}

	public void setNewquantity(String newquantity) {
		this.newquantity = newquantity;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getProductcapacity() {
		return productcapacity;
	}

	public void setProductcapacity(String productcapacity) {
		this.productcapacity = productcapacity;
	}

	public String getActionType() {
		return actionType;
	}

	public void setActionType(String actionType) {
		this.actionType = actionType;
	}

	public int getRtweight() {
		return rtweight;
	}

	public void setRtweight(int rtweight) {
		this.rtweight = rtweight;
	}

	public int getRtquantity() {
		return rtquantity;
	}

	public void setRtquantity(int rtquantity) {
		this.rtquantity = rtquantity;
	}

	public int getSeqno() {
		return seqno;
	}

	public void setSeqno(int seqno) {
		this.seqno = seqno;
	}

	public String getJichikucd() {
		return jichikucd;
	}

	public void setJichikucd(String jichikucd) {
		this.jichikucd = jichikucd;
	}

}
